#include "BbsControlData.h"
#include "Edit.h"
#include "Output.h"
#include "Input.h"
#include "StringTable.h"
#include "Session.h"

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <device.h>

#pragma code-name("EDITCODE");
#pragma rodata-name("EDITCODE");
#pragma data-name("EDITDATA");
#pragma bss-name("EDITBSS");

void editMessage(void);
void saveCurrentThread(void);

void enterEditor(void)
{
	//if(_editInfo.filename[0] == 0)
	//{
	//	outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), "No filename given.  Aborting edit.");

	//	_editInfo.aborted = true;

	//	return;
	//}

	editMessage();
}

void editMessage(void)
{
#define LINE_BUFFER_SIZE 81
	static FILE *file;
	static char oldDirectory[17];
	static char directory[17];
	static char filename[17];
	static msgStatus_t msgStatus;
	static char currentLineNumber, i;
	static char lineBuffer[LINE_BUFFER_SIZE];

	if(_editInfo.message != NULL)
	{
		outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), EDITOR_ENTER_SUBJECT_1);
		outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), EDITOR_ENTER_SUBJECT_2);
		outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), EDITOR_ENTER_SUBJECT_3);
		outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), EDITOR_ENTER_SUBJECT_4);

		if(_currentSession.requestedWidth > 40)
		{
			input(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), IO_ANY_CHARACTER, _editInfo.message->subject, 70, false);
		}
		else
		{
			input(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), IO_ANY_CHARACTER, _editInfo.message->subject, 36, false);
		}

		outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), CRLF CRLF);

		outputFile(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), "edit.menu", &(bcd.dataDirectory));

		currentLineNumber = 0;
		while(currentLineNumber < 100
			&& strncmp(lineBuffer, ".a",2) != 0
			&& strncmp(lineBuffer, ".s",2) != 0
			&& strncmp(lineBuffer, "/a",2) != 0
			&& strncmp(lineBuffer, "/s",2) != 0)
		{
			memset(lineBuffer, 0, LINE_BUFFER_SIZE);
			input(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), (inputOptions_t)(IO_ANY_CHARACTER), lineBuffer, _currentSession.requestedWidth - 2, false);
			strcat(lineBuffer, CRLF);

			if(lineBuffer[0] != '.' && lineBuffer[0] != '/')
			{
				memset(_editBuffer.buffer[currentLineNumber], 0, LINE_BUFFER_SIZE);
				strcpy(_editBuffer.buffer[currentLineNumber++], lineBuffer);
			}
			outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), CRLF);
		}

		if(strncmp(lineBuffer, ".a",2) == 0
			|| strncmp(lineBuffer, "/a",2) == 0)
		{
			outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), EDITOR_ABORTING);
			_editInfo.aborted = true;
		}
		else
		{
			outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), EDITOR_SAVING);

			getcwd(oldDirectory, 17);
			getdevicedir(bcd.dataDirectory.device, directory, 17);
			chdir(directory);

			sprintf(filename, FILEPATTERN_MSG, _currentSubboard.subboardNumber, _currentMsgThread.threadNumber, _currentMsg.messageNumber);
			file = fopen(filename, "wb");

			if(file != NULL &&
				fwrite(&_currentMsg, sizeof(message_t), 1, file) == 1)
			{
				msgStatus.lastUpdatedBy = _currentUser.usernumber;
				msgStatus.lastUpdatedOn = _currentMsg.postedOn;
				msgStatus.status = MS_NORMAL;

				if(fwrite(&msgStatus, sizeof(msgStatus_t), 1, file) == 1)
				{
					printf("\ncurrentLineNumber: %u", currentLineNumber);
					for(i = 0; i < currentLineNumber; ++i)
					{
						fwrite(_editBuffer.buffer[i], strlen(_editBuffer.buffer[i]), 1, file);
						printf("\n%s", _editBuffer.buffer[i]);
						outputCharacter(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), '.');
					}

					outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), SUB_MSG_POSTED);
				}

			}
			else
			{
				outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), CRLF CRLF "COULDN'T SAVE");
				
			}

			fclose(file);

			++_currentMsgThread.highMessageNumber;

			saveCurrentThread();
		}
	}
	else
	{
		outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), "No message given.  Aborting edit.");
	}

	if(!_editInfo.aborted)
	{
	}
	else
	{
		outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), SUB_MSG_ABORTED);
	}
}

void saveCurrentThread(void)
{
	char threadTopic[71];
	static FILE *file;
	static char oldDirectory[17];
	static char directory[17];
	static char filename[17];

	getcwd(oldDirectory, 17);
	getdevicedir(bcd.dataDirectory.device, directory, 17);
	chdir(directory);

	sprintf(filename, FILEPATTERN_THREAD, _currentSubboard.subboardNumber, _currentMsgThread.threadNumber);
	file = fopen(filename, "wb");

	if(file != NULL && 
		fwrite(&_currentMsgThread, sizeof(msgThread_t), 1, file) != 1)
	{
		//outputString(PLATFORM_ENCODING, (outDirections_t)(OD_MODEM | OD_SCREEN), CRLF CHX_RED "Could not write thread." CRLF);
	}

	fclose(file);

	chdir(oldDirectory);
}
